﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlCustomer : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (ViewState["flag"] == null)
            {
                //ViewState["flag"] = false;
                ClearAdd();
            }

            BindTinhThanh();

            BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());

            //nằm trong thread
            ThreadStart thrstar = new ThreadStart(BindGender);
            Thread tr = new Thread(thrstar);
            tr.Start();
            // năm trong thread

            tr.Join();
        }
    }

    void ClearAdd()
    {
        TabContainer1.ActiveTabIndex = 0;
        txtMaSo.Text = "New";
        txtDiaChiLienLac.Text = "";
        txtDiaChiThuongTru.Text = "";
        txtHoTen.Text = "";
        txtMemoryDate.Text = "";
        txtMobile.Text = "";
        txtNgaySinh.Text = "";
        txtNgheNghiep.Text = "";
        txtSoCMND.Text = "";
        ViewState["flag"] = true;

        btnSave.Text = "Lưu";
        lblMsg.Text = "";
        lblMsg.Visible = false;
        txtHoTen.Focus();        
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ClearAdd();        
    }

    //protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    //{
    //    dt = new ClassExecute.ClassExecute();
    //    int i=0;
    //    try
    //    {
    //        if ((bool)ViewState["flag"])
    //        {
    //            //insert
    //            dt = new ClassExecute.ClassExecute();
    //            i = dt.ExecuteSQl_int(str, "k_insertArcust", CommandType.StoredProcedure, ref error,
    //                new SqlParameter("@customer_name", txtHoTen.Text),
    //                new SqlParameter("@address", txtDiaChiLienLac.Text),
    //                new SqlParameter("@IDCardNo", txtSoCMND.Text),
    //                new SqlParameter("@Birthday", txtNgaySinh.Text),
    //                new SqlParameter("@Memorydate", txtMemoryDate.Text),
    //                new SqlParameter("@Gender", ddlXungHo.SelectedValue.ToString()),
    //                new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
    //                new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
    //                new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
    //                new SqlParameter("@Career", txtNgheNghiep.Text),
    //                new SqlParameter("@phone", txtMobile.Text),
    //                new SqlParameter("@sitecode",Session["sitecode"].ToString()));
    //            if (i == 1)
    //            {
                   
    //                ShowMessageBox("Thêm mới khách hàng thành công");

    //                ViewState["flag"] = false;

    //                txtMaSo.Text = "";
    //                txtDiaChiLienLac.Text = "";
    //                txtDiaChiThuongTru.Text = "";
    //                txtHoTen.Text = "";
    //                txtMemoryDate.Text = "";
    //                txtMobile.Text = "";
    //                txtNgaySinh.Text = "";
    //                txtNgheNghiep.Text = "";
    //                txtSoCMND.Text = "";

    //            }
    //            else
    //            {
    //                ShowMessageBox("Hệ thống đang bảo trì");
    //            }
    //        }
    //        else
    //        {
    //            //update
    //            dt = new ClassExecute.ClassExecute();
    //            i = dt.ExecuteSQl_int(str, "[k_updateArcust]", CommandType.StoredProcedure, ref error,
    //                new SqlParameter("@customer_name", txtHoTen.Text),
    //                new SqlParameter("@address", txtDiaChiLienLac.Text),
    //                new SqlParameter("@IDCardNo", txtSoCMND.Text),
    //                new SqlParameter("@Birthday", txtNgaySinh.Text),
    //                new SqlParameter("@Memorydate", txtMemoryDate.Text),
    //                new SqlParameter("@Gender", ddlXungHo.SelectedItem.Text),
    //                new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
    //                new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
    //                new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
    //                new SqlParameter("@Career", txtNgheNghiep.Text),
    //                new SqlParameter("@phone", txtMobile.Text),
    //                new SqlParameter("@customer_key",Int32.Parse(txtMaSo.Text)));
    //            if (i == 1)
    //            {
    //                ShowMessageBox("Cập nhật khách hàng thành công");
    //            }
    //            else
    //            {
    //                ShowMessageBox("Đề nghị cung cấp mã khách hàng để cập nhật");
    //            }

    //        }
    //    }
    //    catch (Exception ex)
    //    {

    //        string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
    //        dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
    //    }   
    //}
    //protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    //{
       
    //}
    //protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    //{
    //    TabContainer1.ActiveTabIndex = 1;
    //}

    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGrid(); 
    }
    protected void grvArcust_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvArcust.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
    protected void ddlTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());
    }
    void BindTinhThanh()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db=dt.Get_Data_DataTable(str,ref error,"k_getTinhThanh",CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlTinhThanh, "MaTinh", "TinhThanh");
    }
    void BindQuanHuyen(string MaTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getQuanHuyen", CommandType.StoredProcedure,
            new SqlParameter("@MaTinh",MaTinh));
        dt.BindDropDownList(db, ddlQuanHuyen, "MaQuan", "QuanHuyen");
    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_searchArcust", CommandType.StoredProcedure,
            new SqlParameter("@para", txtSearch.Text),
            new SqlParameter("@sitecode",Session["sitecode"].ToString()));
       

        if (db.Rows.Count == 0)
        {
            grvArcust.DataSource = null;
            ShowMessageBox("Không tìm thấy kết quả");
        }
        else
        {
            grvArcust.DataSource = db;
        }
        grvArcust.DataBind();
    }
    protected void grvArcust_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         dt=new ClassExecute.ClassExecute();
         try
         {
             if (e.CommandName == "chon")
             {

                 GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                 Label lblMaSo = (Label)grv.FindControl("lblMaSo");
                 CheckBox chk = (CheckBox)grv.FindControl("checkActive");

                 DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailArcust", CommandType.StoredProcedure,
                     new SqlParameter("@customer_key",Int32.Parse(lblMaSo.Text)));

                 txtMaSo.Text = db.Rows[0]["customer_key"].ToString();
                 txtHoTen.Text = db.Rows[0]["customer_name"].ToString(); 
                 txtDiaChiLienLac.Text=db.Rows[0]["address"].ToString(); 
                 txtSoCMND.Text=db.Rows[0]["IDCardNo"].ToString(); 
                 txtNgaySinh.Text=db.Rows[0]["Birthday"].ToString(); 
                 txtMemoryDate.Text=db.Rows[0]["Memorydate"].ToString();

                 chkActive.Checked = chk.Checked;

                 try
                 {
                     ddlXungHo.SelectedValue=db.Rows[0]["Gender"].ToString(); 
                 }
                 catch
                 {

                 }
                 txtDiaChiThuongTru.Text=db.Rows[0]["ThuongTru"].ToString(); 
                  try
                 {
                      
                     ddlTinhThanh.SelectedValue=db.Rows[0]["ProvinceCode"].ToString(); 
                 }
                 catch
                 {

                 }
                  try
                 {
                      
                     ddlQuanHuyen.SelectedValue=db.Rows[0]["TownCode"].ToString(); 
                 }
                 catch
                 {

                 }
                 txtNgheNghiep.Text=db.Rows[0]["Career"].ToString(); 
                 txtMobile.Text=db.Rows[0]["phone"].ToString();

                 grvArcust.DataSource = null;
                 grvArcust.DataBind();

                 ViewState["flag"] = false;
                 TabContainer1.ActiveTabIndex = 0;

                 lblMsg.Visible = false;
                 btnSave.Text = "Cập nhật";
             }
         }
         catch (Exception ex)
         {

             string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
             dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
         }   
    }
    void BindGender()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getListGender", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlXungHo, "GenderID", "GenderName");
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                dt = new ClassExecute.ClassExecute();
                i = dt.ExecuteSQl_int(str, "k_insertArcust", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@customer_name", txtHoTen.Text),
                    new SqlParameter("@address", txtDiaChiLienLac.Text),
                    new SqlParameter("@IDCardNo", txtSoCMND.Text),
                    new SqlParameter("@Birthday", txtNgaySinh.Text),
                    new SqlParameter("@Memorydate", txtMemoryDate.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedValue.ToString()),
                    new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
                    new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Career", txtNgheNghiep.Text),
                    new SqlParameter("@phone", txtMobile.Text),
                    new SqlParameter("@sitecode", Session["sitecode"].ToString()),
                    new SqlParameter("@Active", chkActive.Checked));
                if (i == 1)
                {

                    //ShowMessageBox("Thêm mới khách hàng thành công");

                    //ViewState["flag"] = false;

                    //txtMaSo.Text = "";
                    //txtDiaChiLienLac.Text = "";
                    //txtDiaChiThuongTru.Text = "";
                    //txtHoTen.Text = "";
                    //txtMemoryDate.Text = "";
                    //txtMobile.Text = "";
                    //txtNgaySinh.Text = "";
                    //txtNgheNghiep.Text = "";
                    //txtSoCMND.Text = "";

                    ClearAdd();
                    lblMsg.Text = "Thêm mới khách hàng thành công";
                    lblMsg.Visible = true;

                }
                else
                {
                    //ShowMessageBox("Hệ thống đang bảo trì");
                    lblMsg.Text = "Hệ thống đang bảo trì";
                    lblMsg.Visible = true;
                }
            }
            else
            {
                //update
                dt = new ClassExecute.ClassExecute();
                i = dt.ExecuteSQl_int(str, "[k_updateArcust]", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@customer_name", txtHoTen.Text),
                    new SqlParameter("@address", txtDiaChiLienLac.Text),
                    new SqlParameter("@IDCardNo", txtSoCMND.Text),
                    new SqlParameter("@Birthday", txtNgaySinh.Text),
                    new SqlParameter("@Memorydate", txtMemoryDate.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedItem.Text),
                    new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
                    new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Career", txtNgheNghiep.Text),
                    new SqlParameter("@phone", txtMobile.Text),
                    new SqlParameter("@customer_key", Int32.Parse(txtMaSo.Text)),
                    new SqlParameter("@Active", chkActive.Checked));
                if (i == 1)
                {
                    //ShowMessageBox("Cập nhật khách hàng thành công");
                    lblMsg.Text = "Cập nhật khách hàng thành công";
                    lblMsg.Visible = true;
                }
                else
                {
                    //ShowMessageBox("Đề nghị cung cấp mã khách hàng để cập nhật");
                    lblMsg.Text = "Đề nghị cung cấp mã khách hàng để cập nhật";
                    lblMsg.Visible = true;
                }

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   

    }   
}